// pages/shop/order/settlement/settlement.js
const app = getApp();
const call = require('../../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tradeItemList: []
  },
  /**
    提交订单
   */
  onSubmit() {
    var that = this;
    // console.log('提交订单...');
    const url = `/service/app/v1/order/checkOrder?access_token=${app.globalData.access_token}`
    const params = {
      "header": {},
      "data": {
        "receiver": {
          "name": "张三",
          "province": "上海",
          "city": "上海市",
          "county": "徐汇区",
          "address": "山东中路1号",
          "mobile": "18513582379",
          "phone": "02187111111"
        },
        "tradeItemList": [{
          "productId": "67569",
          "activityId": "C0F21DDC495891A43C748E28587B3E3B",
          "source": "nanjingningyue_1690",
          "price": "0.01",
          "qty": 1
        }],
        "orderSource": 0,
        "orderChannel": 0
      }
    };
    call.postData(url, params,
      res => {
        console.log('订单校验接口调用成功:', res);
        const tradeList1 = res.data.tradelist[0];
        const tradeList = {
          source: tradeList1.source,
          tradeItemList: tradeList1.tradeItemList
        }
        that._submitOrder(tradeList);
      },
      err => {
        console.log('订单校验接口调用失败', err)
      });
    console.log('提交的参数: ', params)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },
  _submitOrder(tradeList) {
    var that = this;
    const url = `/service/app/v1/order/submitOrder?access_token=${app.globalData.access_token}`;
    const params = {
      "header": {},
      "data": {
        "invoiceName": "张三发票名称",
        "invoiceContent": "食品",
        "invoiceType": 1,
        "invoiceWay": 2,
        "settlementType": 1,
        "receiver": {
          "name": "张三",
          "province": "上海",
          "city": "上海市",
          "county": "徐汇区",
          "address": "山东中路1号",
          "mobile": "18513582379",
          "phone": "02187111111"
        },
        "tradeList": [tradeList],
        "orderSource": 0,
        "orderChannel": 0,
        // "orderTrackCode": "T_20180224134104_73654f6e-125d-433f-afdf-ca8d6b7bf31a"
      }
    };
    call.postData(url, params,
      res => {
        console.log('订单提交接口成功', res);
        if (res.msg === '') {
          wx.navigateTo({
            url: `/pages/shop/order/payOrder/payOrder?orderGroupId=${res.data.orderGroupId}&&paymentAmount=${res.data.paymentAmount}`,
          })
        } else {
          wx.showToast({
            title: res.msg
          })
        }
      },
      err => {
        console.log('订单提交接口失败', err);
      })

  }
})